package com.sundi.ecmarket.web;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.ecmarket.dto.AfterDto;
import com.sundi.ecmarket.dto.BrandDto;
import com.sundi.ecmarket.dto.ResultEntity;
import com.sundi.ecmarket.entity.EcAftersale;
import com.sundi.ecmarket.service.IEcAftersaleService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 售后表 前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-23
 */
@RestController
@RequestMapping("/ecAftersale")
public class EcAftersaleController {


    @Autowired
    IEcAftersaleService iEcAftersaleService;


    /**
     * @deprecated ec商城中商城管理之售后管理
     * @param afterDto
     * @return
     */
    @RequestMapping("/xujiabao/afterlist")
    public ResultEntity list(AfterDto afterDto){

        System.err.println(afterDto);
        PageHelper.startPage(afterDto.getPageNo(),afterDto.getPageSize());
        List<EcAftersale> ecAftersales = iEcAftersaleService.selectAfterList(afterDto);
        PageInfo info = new PageInfo(ecAftersales);
        return ResultEntity.ok(info);
    }

    @RequestMapping("/xujiabao/plUpd")
    @Transactional
    public ResultEntity plUpd(Integer[] ids){
        List<EcAftersale> list = new ArrayList<EcAftersale>();
        for (Integer id: ids) {
            EcAftersale ecAftersale = new EcAftersale();
            ecAftersale.setId(id);
            ecAftersale.setStatus(2);
            if(ecAftersale.getStatus()==2){
                ecAftersale.setUpdateTime(new Date());
            }
            list.add(ecAftersale);
        }
        iEcAftersaleService.updateBatchById(list);
        return ResultEntity.ok();
    }

    @RequestMapping("/xujiabao/plNo")
    @Transactional
    public ResultEntity plNo(Integer[] ids){
        List<EcAftersale> list = new ArrayList<EcAftersale>();
        for (Integer id: ids) {
            EcAftersale ecAftersale = new EcAftersale();
            ecAftersale.setId(id);
            ecAftersale.setStatus(4);
            if(ecAftersale.getStatus()==4){
                ecAftersale.setAddTime(new Date());
            }
            list.add(ecAftersale);
        }
        iEcAftersaleService.updateBatchById(list);
        return ResultEntity.ok();
    }

    /**
     * @deprecated ec商城中商场管理里的售后管理逻辑删除模块
     * @author 许家宝
     * @param id
     * @return
     */
    @RequestMapping("/xujiabao/logicdel")
    public ResultEntity logindel(Integer id){

        System.err.println(id);
        iEcAftersaleService.updateLogicDel(id);
        return ResultEntity.ok();
    }

    @RequestMapping("/downLoadGoodsExcel")
    public ResponseEntity downLoadGoodsExcel(AfterDto afterDto) throws Exception {

        XSSFWorkbook xSSFWorkbooks = iEcAftersaleService.downLoadGoodsExcel(afterDto);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        xSSFWorkbooks.write(byteArrayOutputStream);

        HttpHeaders httpHeaders =  new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", new String("售后表.xlsx".getBytes("UTF-8"), "ISO-8859-1"));
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity(byteArrayOutputStream.toByteArray(),httpHeaders, HttpStatus.CREATED);

    }
}
